import Foundation

class AuthService {
    static let shared = AuthService()
    
    private init() {}
    
    // MARK: - User Authentication
    
    func login(username: String, password: String) async throws -> LoginResponse {
        let request = LoginRequest(username: username, password: password)
        let jsonData = try JSONEncoder().encode(request)
        
        return try await NetworkManager.shared.performRequest(endpoint: "/auth/login", method: .post, body: jsonData)
    }
    
    func register(username: String, password: String, phone: String? = nil, email: String? = nil) async throws -> RegisterResponse {
        let request = RegisterRequest(username: username, password: password, phone: phone, email: email)
        let jsonData = try JSONEncoder().encode(request)
        
        return try await NetworkManager.shared.performRequest(endpoint: "/auth/register", method: .post, body: jsonData)
    }
    
    // MARK: - User Profile
    
    func getUserProfile() async throws -> UserProfileResponse {
        return try await NetworkManager.shared.performRequest(endpoint: "/users/profile")
    }
    
    // MARK: - Save/Get Auth Token
    
    func saveAuthToken(_ token: String) {
        UserDefaults.standard.set(token, forKey: "authToken")
    }
    
    func getAuthToken() -> String? {
        return UserDefaults.standard.string(forKey: "authToken")
    }
    
    func clearAuthToken() {
        UserDefaults.standard.removeObject(forKey: "authToken")
    }
}

// MARK: - Auth Request Models

struct LoginRequest: Codable {
    let username: String
    let password: String
}

struct RegisterRequest: Codable {
    let username: String
    let password: String
    let phone: String?
    let email: String?
}

// MARK: - Auth Response Models

struct LoginResponse: Codable {
    let code: Int
    let message: String
    let data: LoginData
}

struct LoginData: Codable {
    let userID: String
    let token: String
    
    enum CodingKeys: String, CodingKey {
        case userID = "user_id"
        case token
    }
}

struct RegisterResponse: Codable {
    let code: Int
    let message: String
    let data: RegisterData
}

struct RegisterData: Codable {
    let userID: String
    let token: String
    
    enum CodingKeys: String, CodingKey {
        case userID = "user_id"
        case token
    }
}

struct UserProfileResponse: Codable {
    let code: Int
    let message: String
    let data: UserProfileData
}

struct UserProfileData: Codable {
    let userID: String
    let username: String
    let nickname: String?
    let avatarURL: String?
    let phone: String?
    let email: String?
    let interestTags: [String]?
    let memberLevel: String?
    let joinDate: String?
    
    enum CodingKeys: String, CodingKey {
        case userID = "user_id"
        case username
        case nickname
        case avatarURL = "avatar_url"
        case phone
        case email
        case interestTags = "interest_tags"
        case memberLevel = "member_level"
        case joinDate = "join_date"
    }
}